package com.sky.service.impl;

import cn.hutool.json.JSONUtil;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<Category> list(Integer type) {
        List<Category> list=null;

        //key的编排 工程名:模块名:业务场景:特征码
        String key="sky:admin:categories:"+type;
        if(redisTemplate.hasKey(key)){
            String jsonStr = (String) redisTemplate.opsForValue().get(key);
            list = JSONUtil.toList(jsonStr, Category.class);
        }else{
            list = categoryMapper.getList();
            //判断type
            if(type!=null){
              list=list.stream().filter(s->s.getType().equals(type)).collect(Collectors.toList());
            }
            String jsonStr = JSONUtil.toJsonStr(list);
            redisTemplate.opsForValue().set(key,jsonStr);
        }

        return list;
    }
}
